<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="预测时间" prop="time">
        <el-date-picker clearable
          v-model="queryParams.time"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择预测时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="站点名称" prop="station">
        <el-input
          v-model="queryParams.station"
          placeholder="请输入站点名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据预测模型[OVMD-TCN-AR单步预测模型、STL-TCN 多步预测模型]" prop="dfm">
        <el-input
          v-model="queryParams.dfm"
          placeholder="请输入数据预测模型[OVMD-TCN-AR单步预测模型、STL-TCN 多步预测模型]"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测步长" prop="ps">
        <el-input
          v-model="queryParams.ps"
          placeholder="请输入预测步长"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测河流氨氮值" prop="fm">
        <el-input
          v-model="queryParams.fm"
          placeholder="请输入预测河流氨氮值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测值_1" prop="fv1">
        <el-input
          v-model="queryParams.fv1"
          placeholder="请输入预测值_1"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测值_2" prop="fv2">
        <el-input
          v-model="queryParams.fv2"
          placeholder="请输入预测值_2"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测值_3" prop="fv3">
        <el-input
          v-model="queryParams.fv3"
          placeholder="请输入预测值_3"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测值_4" prop="fv4">
        <el-input
          v-model="queryParams.fv4"
          placeholder="请输入预测值_4"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测值_5" prop="fv5">
        <el-input
          v-model="queryParams.fv5"
          placeholder="请输入预测值_5"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测值_6" prop="fv6">
        <el-input
          v-model="queryParams.fv6"
          placeholder="请输入预测值_6"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测值_7" prop="fv7">
        <el-input
          v-model="queryParams.fv7"
          placeholder="请输入预测值_7"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测值_8" prop="fv8">
        <el-input
          v-model="queryParams.fv8"
          placeholder="请输入预测值_8"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测值_9" prop="fv9">
        <el-input
          v-model="queryParams.fv9"
          placeholder="请输入预测值_9"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测值_10" prop="fv10">
        <el-input
          v-model="queryParams.fv10"
          placeholder="请输入预测值_10"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测值_1" prop="fv11">
        <el-input
          v-model="queryParams.fv11"
          placeholder="请输入预测值_1"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预测值_12" prop="fv12">
        <el-input
          v-model="queryParams.fv12"
          placeholder="请输入预测值_12"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['pred:pred:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pred:pred:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pred:pred:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['pred:pred:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="predList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="水质预测结果编号" align="center" prop="id" />
      <el-table-column label="预测时间" align="center" prop="time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="站点名称" align="center" prop="station" />
      <el-table-column label="数据预测模型[OVMD-TCN-AR单步预测模型、STL-TCN 多步预测模型]" align="center" prop="dfm" />
      <el-table-column label="预测步长" align="center" prop="ps" />
      <el-table-column label="预测河流氨氮值" align="center" prop="fm" />
      <el-table-column label="预测值_1" align="center" prop="fv1" />
      <el-table-column label="预测值_2" align="center" prop="fv2" />
      <el-table-column label="预测值_3" align="center" prop="fv3" />
      <el-table-column label="预测值_4" align="center" prop="fv4" />
      <el-table-column label="预测值_5" align="center" prop="fv5" />
      <el-table-column label="预测值_6" align="center" prop="fv6" />
      <el-table-column label="预测值_7" align="center" prop="fv7" />
      <el-table-column label="预测值_8" align="center" prop="fv8" />
      <el-table-column label="预测值_9" align="center" prop="fv9" />
      <el-table-column label="预测值_10" align="center" prop="fv10" />
      <el-table-column label="预测值_1" align="center" prop="fv11" />
      <el-table-column label="预测值_12" align="center" prop="fv12" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['pred:pred:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['pred:pred:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改水质预测结果数据对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="predRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预测时间" prop="time">
          <el-date-picker clearable
            v-model="form.time"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择预测时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="站点名称" prop="station">
          <el-input v-model="form.station" placeholder="请输入站点名称" />
        </el-form-item>
        <el-form-item label="数据预测模型[OVMD-TCN-AR单步预测模型、STL-TCN 多步预测模型]" prop="dfm">
          <el-input v-model="form.dfm" placeholder="请输入数据预测模型[OVMD-TCN-AR单步预测模型、STL-TCN 多步预测模型]" />
        </el-form-item>
        <el-form-item label="预测步长" prop="ps">
          <el-input v-model="form.ps" placeholder="请输入预测步长" />
        </el-form-item>
        <el-form-item label="预测河流氨氮值" prop="fm">
          <el-input v-model="form.fm" placeholder="请输入预测河流氨氮值" />
        </el-form-item>
        <el-form-item label="预测值_1" prop="fv1">
          <el-input v-model="form.fv1" placeholder="请输入预测值_1" />
        </el-form-item>
        <el-form-item label="预测值_2" prop="fv2">
          <el-input v-model="form.fv2" placeholder="请输入预测值_2" />
        </el-form-item>
        <el-form-item label="预测值_3" prop="fv3">
          <el-input v-model="form.fv3" placeholder="请输入预测值_3" />
        </el-form-item>
        <el-form-item label="预测值_4" prop="fv4">
          <el-input v-model="form.fv4" placeholder="请输入预测值_4" />
        </el-form-item>
        <el-form-item label="预测值_5" prop="fv5">
          <el-input v-model="form.fv5" placeholder="请输入预测值_5" />
        </el-form-item>
        <el-form-item label="预测值_6" prop="fv6">
          <el-input v-model="form.fv6" placeholder="请输入预测值_6" />
        </el-form-item>
        <el-form-item label="预测值_7" prop="fv7">
          <el-input v-model="form.fv7" placeholder="请输入预测值_7" />
        </el-form-item>
        <el-form-item label="预测值_8" prop="fv8">
          <el-input v-model="form.fv8" placeholder="请输入预测值_8" />
        </el-form-item>
        <el-form-item label="预测值_9" prop="fv9">
          <el-input v-model="form.fv9" placeholder="请输入预测值_9" />
        </el-form-item>
        <el-form-item label="预测值_10" prop="fv10">
          <el-input v-model="form.fv10" placeholder="请输入预测值_10" />
        </el-form-item>
        <el-form-item label="预测值_1" prop="fv11">
          <el-input v-model="form.fv11" placeholder="请输入预测值_1" />
        </el-form-item>
        <el-form-item label="预测值_12" prop="fv12">
          <el-input v-model="form.fv12" placeholder="请输入预测值_12" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Pred">
import { listPred, getPred, delPred, addPred, updatePred } from "@/api/pred/pred"

const { proxy } = getCurrentInstance()

const predList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    time: null,
    station: null,
    dfm: null,
    ps: null,
    fm: null,
    fv1: null,
    fv2: null,
    fv3: null,
    fv4: null,
    fv5: null,
    fv6: null,
    fv7: null,
    fv8: null,
    fv9: null,
    fv10: null,
    fv11: null,
    fv12: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询水质预测结果数据列表 */
function getList() {
  loading.value = true
  listPred(queryParams.value).then(response => {
    predList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    time: null,
    station: null,
    dfm: null,
    ps: null,
    fm: null,
    fv1: null,
    fv2: null,
    fv3: null,
    fv4: null,
    fv5: null,
    fv6: null,
    fv7: null,
    fv8: null,
    fv9: null,
    fv10: null,
    fv11: null,
    fv12: null,
    createTime: null,
    updateTime: null
  }
  proxy.resetForm("predRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加水质预测结果数据"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getPred(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改水质预测结果数据"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["predRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePred(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPred(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除水质预测结果数据编号为"' + _ids + '"的数据项？').then(function() {
    return delPred(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('pred/pred/export', {
    ...queryParams.value
  }, `pred_${new Date().getTime()}.xlsx`)
}

getList()
</script>
